﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using RN.ERP.Web.ModelService;
using Fly.Common.Linq;

namespace RN.ERP.Web.Enterprise
{
    public partial class EnterpriseCorpWDForCpp : System.Web.UI.Page
    {
        protected string CorpUserGuid
        {
            get
            {
                return Request["corpUserGuid"] == null ? "" : Request["corpUserGuid"];
            }
        }
        protected string AreaCode
        {
            get
            {
                return Request["areaCode"] == null ? "" : Request["areaCode"];
            }
        }
        private EnterpriseService.EnterpriseServiceClient enterpriseServiceClient = new EnterpriseService.EnterpriseServiceClient();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (string.IsNullOrEmpty(AreaCode))
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('地区为空，无法确定市场范围')", true);
                    return;
                }
                InitPage();
                BindData();
                BindFiles();
            }
            
        }
        private void InitPage()
        {
            hdnCorpUserGuid.Value = CorpUserGuid;
        }
        private void BindData()
        {
            if (!string.IsNullOrEmpty(CorpUserGuid))
            {
                var currentAreaCode = AreaCode;
                ////获取企业信息，登陆账户与企业不一致
                string corpUserGuid = CorpUserGuid;
                var corpInfo = GetCompanyinfo(corpUserGuid);
                if (corpInfo == null)
                {
                    return;
                }
                ltrCorpName.Text = corpInfo.CorpName;
                var embrabchMent = JsonConvert.DeserializeObject<List<Tb_Corp_Embranchment>>(enterpriseServiceClient.GetCorpEmbranchments(JsonConvert.SerializeObject(corpInfo)));
                ////分支机构信息
                hdnEmbranchCorpUserGuid.Value = corpInfo.CorpUserGuid.ToString();
                hdnEmbranchCorpGuid.Value = corpInfo.CorpGuid.ToString();
                hdnEmbranchAreaCode.Value = currentAreaCode;
                var currentEmbranchment = embrabchMent.Where(e => e.AreaCode.StartsWith(currentAreaCode)).FirstOrDefault();
                var registerAreaCode = corpInfo.AreaCode.Length > 2 ? corpInfo.AreaCode.Substring(0, 2) : corpInfo.AreaCode;
                if (currentEmbranchment != null && !currentEmbranchment.AreaCode.StartsWith(registerAreaCode))
                {
                    txtEmbranchmentAddress.Text = currentEmbranchment.Address;
                    txtEmbranchmentLinkMan.Text = currentEmbranchment.LinkMan;
                    txtEmbranchmentLinkTel.Text = currentEmbranchment.LinkTel;
                    txtEmbranchmentRemark.Text = currentEmbranchment.Remark;
                    txtEmbranchmentTel.Text = currentEmbranchment.Tel;
                    txtEmbranchmentLicenceCode.Text = currentEmbranchment.licenceCode;
                    txtBranchName.Text = currentEmbranchment.BranchName;
                    hdnEmbranchGid.Value = currentEmbranchment.BranchGid.ToString();
                }
                else
                {
                    ltrMsg.Visible = true;
                }
            }
        }

        private Tb_Corp_CompanyInfo GetCompanyinfo(string corpUserGuid)
        {
            QueryCompanyInfoCondition queryCompanyInfoCondition =
               new QueryCompanyInfoCondition() { CorpUserGuid = new ParameterValue<Guid>() { V = Guid.Parse(corpUserGuid), O = OptEnum.Equal } };
            List<QueryCompanyInfoCondition> queryCompanyInfoList = new List<QueryCompanyInfoCondition>();
            queryCompanyInfoList.Add(queryCompanyInfoCondition);
            var corpInfo = JsonConvert.DeserializeObject<Tb_Corp_CompanyInfo>(enterpriseServiceClient.GetCompanyInfo(JsonConvert.SerializeObject(queryCompanyInfoList), AreaCode));
            return corpInfo;
        }

        public string OpenNewPageShowFile(object fileName)
        {
            string sysCode = "EF";
            string funcCode = "CorpWDZR";
            return string.Format("UploadFileShowNewpage.aspx?fileName={0}&sysCode={1}&funcCode={2}&rand ={3}", fileName, sysCode, funcCode, Guid.NewGuid());
        }

        /// <summary>
        /// 绑定所有资质扫描件
        /// </summary>
        private void BindFiles()
        {
            string sysCode = "EF";
            string funcCode = "CorpWDZR";
            string objectGuid = hdnEmbranchGid.Value;
            if ((!string.IsNullOrEmpty(objectGuid)) && (!string.IsNullOrEmpty(sysCode)) && (!string.IsNullOrEmpty(funcCode)))
            {
                using (FileService.FileWebServiceSoapClient client = new FileService.FileWebServiceSoapClient())
                {
                    FileService.ArrayOfString files = client.GetFileList(objectGuid, sysCode, funcCode);
                    rptImgs.DataSource = files;
                    rptImgs.DataBind();
                }
            }
        }

        public string GetImgUrl(object fileName)
        {
            string sysCode = "EF";
            string funcCode = "CorpWDZR";
            return string.Format("../Handlers/GetFileHandler.ashx?fileName={0}&sysCode={1}&funcCode={2}&rand ={3}", fileName, sysCode, funcCode, Guid.NewGuid());
        }
    }
}